package co.example.michael.bigpictureloading;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.ResponseHandlerInterface;

import org.apache.http.Header;

import co.example.michael.tools.InternetTools;

/**
 * Created by Michael on 2015/3/20.
 */
public class NetworkActivity extends Activity {

    private EditText editText;

    private Button uploadButton;

    private InternetTools internetTools;

    private AsyncHttpClient asyncHttpClient;

    private TextView jsonText;

    private Button downloadButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.network_layout);

        internetTools = InternetTools.getInstance();
        asyncHttpClient = new AsyncHttpClient();

        editText = (EditText) findViewById(R.id.edit_text);
        jsonText = (TextView)findViewById(R.id.json_text);
        uploadButton = (Button) findViewById(R.id.upload_button);
        uploadButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(NetworkActivity.this,"点击了",Toast.LENGTH_LONG).show();
                uploadButtonListener();
            }
        });

        downloadButton = (Button)findViewById(R.id.download_button);
        downloadButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                downloadButtonListener();
            }
        });
    }

    /**
     * 从网络上获取数据的测试
     * Get 请求
     */
    private void downloadButtonListener() {
        Log.e("NetWorkActivity:","请求数据了");
        String strUrl = "https://api.weibo.com/2/statuses/public_timeline.json?access_token=2.00UZLiWD0sfgia4e16d208e80gcGFt&count=1";
        asyncHttpClient.get(NetworkActivity.this,strUrl,new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                String jsonData = new String(bytes);
                Log.e("从服务器返回的数据：",jsonData);
                jsonText.setText(jsonData);
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                throwable.printStackTrace();
            }
        });
    }

    /**
     * 给服务器上传数据
     * Post 请求
     */
    private void uploadButtonListener(){
        String uploadUrl = "https://api.weibo.com/2/statuses/update.json";

        RequestParams params = new RequestParams();
        params.put("access_token","2.00UZLiWD0sfgia4e16d208e80gcGFt");
        params.put("status","测试测试测测测测");

        asyncHttpClient.post(NetworkActivity.this,uploadUrl,params,new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                Log.e("返回码：",""+i);
                String reposeData = new String(bytes);
                Log.e("返回的数据：",reposeData);
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                throwable.printStackTrace();
            }
        });
    }
}
